package com.itheima.gateway.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangyan
 * @date 2021/3/20 23:46
 */
@Slf4j
@RestController
@RequestMapping("/alarm")
public class AlarmController {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.from}")
    private String from;

    /**
     * 接收skyWalking发送的告警通知并发送至邮箱
     */
    @PostMapping("/receive")
    public void sendMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        // 发送者邮箱
        message.setFrom(from);
        // 接收者邮箱
        message.setTo("510320359@163.com");
        // 主题
        message.setSubject("告警邮件");
        // 邮件内容
        message.setText("来自skyWalking的告警信息");
        mailSender.send(message);
        log.info("告警邮件已发送...");
    }
}
